package com.example.codegen.resolver;

import com.example.codegen.model.Column;
import com.example.codegen.model.Datasource;
import com.example.codegen.model.Table;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.util.List;
import java.util.UUID;

public interface BaseResolver {

    default Configuration getConfiguration(Datasource datasource) {
        UnpooledDataSource unpooledDataSource = new UnpooledDataSource(datasource.getDatabaseDriver(), datasource.getDatabaseUrl(), datasource.getUsername(), datasource.getPassword());
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment(UUID.randomUUID().toString(), transactionFactory, unpooledDataSource);
        Configuration configuration = new Configuration(environment);
        configuration.setMapUnderscoreToCamelCase(true);
        return configuration;
    }

    SqlSession getSession(Datasource datasource);

    String getVersion(Datasource datasource);

    List<Table> getTables(Datasource datasource);

    Table getTable(Datasource datasource, String tableName);

    List<Column> getColumns(Datasource datasource, String tableName);
}
